package org.example.springboot_demo1.controllerRequest;

import org.example.springboot_demo1.controllerRequest.person.Person;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/HelloController")
//@RestController声明该类是一个Spring MVC控制器
@RestController
public class HelloController {
    //不接收参数
    //作为 方法注解 时,@RequestMapping(value = "/hello",method = RequestMethod.GET)等同于@GetMapping(value = "/hello")
    //设置返回的响应是json格式,produces = "application/json"
    @RequestMapping(value = "/hello",method = RequestMethod.GET,produces = "application/json")
    public String hello() {
        return "{\"Hello\" : World}";
    }
    //1.一个参数
    @RequestMapping("/receiveAge1")
    //不传参默认为null
    public String receiveAge1(Integer age) {
        return "接收到参数 age:" + age;
    }
    @RequestMapping("/receiveAge2")
    //不传参会报错
    public String receiveAge2(int age) {
        return "接收到参数 age:" + age;
    }
    @RequestMapping("/receiveRename")
    //@RequestParam将url中key=name的查询字符串绑定到控制器的userName参数上
    //required = false设置该参数为非必传(默认为true,必传)
    public String receiveRename(@RequestParam(value = "name",required = false) String userName) {
        return "接收到参数name:" + userName;
    }
    //2.两个参数
    @RequestMapping("/receiveName_Age")
    public String receiveAge(String name,Integer age) {
        return "接收到参数 name:" + name + " age:" + age;
    }
    //3.接收一个对象
    @RequestMapping("/receivePerson")
    //@RequestBody接收JSON格式的数据
    public String receivePerson(@RequestBody Person person) {
        return "接收到参数 person:" + person;
    }
    //4.接收数组
    @RequestMapping("/receiveArray")
    public String receiveArray(String[] array) {
        return "接收到参数 array:" + Arrays.toString(array);
    }
    //5.接收集合类
    @RequestMapping("/receiveList1")
    public String receiveList1(ArrayList<String> list) {
        //返回的list为空
        return "接收到参数 list:" + list;
    }
    @RequestMapping("/receiveList2")
    public String receiveList2(@RequestParam(required = false) ArrayList<String> list) {
        //正确返回
        return "接收到参数 list:" + list;
    }
    @RequestMapping("/receiveList3")
    public String receiveList3(List<String> list) {
        //报错
        return "接收到参数 list:" + list;
    }
    //6.从路径中获取参数
    @RequestMapping("/receivePath/{article}/{blog}")
    //required = false设置该参数为非必传(默认为true,必传)
    public String receivePath(@PathVariable(value = "article",required = false)String title,@PathVariable(value = "blog",required = false)String content) {
        return "接收到参数 article:" + title + " blog:" + content;
    }
    //7.接收文件
    @RequestMapping("/receiveFile")
    public String receiveFile(@RequestPart(value = "file",required = false) MultipartFile imgFile,@RequestParam(value = "userName",required = false) String name) {
        //返回原始的文件名
        return "用户:" + name+ ",接收到文件:" +imgFile.getOriginalFilename();
    }
}
